//
//  MeatOrderBox.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/29.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderBox.h"
#import "MeatShopCartTableViewCell.h"


//展示Viwe高度
#define kCategoryHeight 421


@interface MeatOrderBox ()<UITableViewDelegate, UITableViewDataSource>


/**
 MeatShopCartBox 's TableView -> UITableView
 */
@property (nonatomic, strong) UITableView *tableView;


/**
 MeatShopCartBox 's Header -> UIView
 */
@property (nonatomic, strong) UIView *header;


@end


@implementation MeatOrderBox

- (void)setupUI{
    
    [super setupUI];
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.contentView.backgroundColor = KClearColor;
    [self.contentView addSubview:self.tableView];
    
}

- (void)setupContentLayout{
    
    {
        self.contentView.mj_x = 0;
        self.contentView.mj_y = KScreenHeight - kCategoryHeight;
        self.contentView.width = KScreenWidth;
        self.contentView.height = kCategoryHeight;
    }
    
    {
        self.tableView.frame = CGRectMake(0, 0, KScreenWidth, kCategoryHeight + TJ_SafeAreaBottomHeight);
    }
}


#pragma mark - tableview delegate/dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 34;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0, 0, KScreenWidth, 34);
    view.backgroundColor = mBackgroudColor;
    
    UILabel *label = [UILabel makeLabelWithText:@"三元猪" textColor:UIColorFromRGB(0x969696) font:mFont(14) textAliment:0];
    label.frame = view.frame;
    label.width = KScreenWidth - 30;
    label.mj_x = 15;
    [view addSubview:label];
    
    return view;
}


- (void)scrollTableViewTop{
    kWeakSelf(self);
    NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:1];
    [weakself.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 86;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeatShopCartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeatShopCartTableViewCell"];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - getter and setter

#pragma mark - setter and getter

- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = KClearColor;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.rowHeight = 86;
        _tableView.tableHeaderView = self.header;
        [_tableView registerClass:[MeatShopCartTableViewCell class] forCellReuseIdentifier:@"MeatShopCartTableViewCell"];
    }
    return _tableView;
}


- (UIView *)header{
    if (!_header) {
        _header = [[UIView alloc] init];
        _header.backgroundColor = KWhiteColor;
        _header.frame = CGRectMake(0, 0, KScreenWidth, 52);
        
        [self drawCornerByView:_header];
        
        
        UILabel *titleLab = [UILabel makeLabelWithText:@"商品清单" textColor:UIColorFromRGB(0x262626) font:mFont(16) textAliment:0];
        titleLab.frame = CGRectMake(15, 0, KScreenWidth/2.0, 52);
        [_header addSubview:titleLab];
        
        
        UILabel *detailLab = [UILabel makeLabelWithText:@"4件" textColor:UIColorFromRGB(0x262626) font:mFont(14) textAliment:2];
        detailLab.frame = CGRectMake(0, 0, KScreenWidth/2.0, 52);
        detailLab.mj_x = KScreenWidth - detailLab.width - 15;
        [_header addSubview:detailLab];

    }
    return _header;
}

- (void)drawCornerByView:(UIView *)view{
    
    CGRect rect = CGRectMake(0, 0, view.width, 52);
    UIRectCorner corners = UIRectCornerTopLeft|UIRectCornerTopRight;
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    _header.layer.mask = shape;
}

#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 34;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}



@end

